package com.yixun.qfbao.controller.agency.weapp;

import com.alibaba.fastjson.JSONObject;
import com.yixun.common.page.Page;
import com.yixun.qfbao.annotations.History;
import com.yixun.qfbao.api.service.AreaService;
import com.yixun.qfbao.api.service.BaseCodeValueService;
import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.PremisesApiService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.common.model.PageDTO;
import com.yixun.qfbao.mapper.PremisesPartnerMapper;
import com.yixun.qfbao.mapper.PremisesImageMapper;
import com.yixun.qfbao.model.BaseCodeValue;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.PremisesPartner;
import com.yixun.qfbao.model.dto.PremisesPartnerDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.TimeUnitTypeEnum;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.request.PageRequest;
import com.yixun.qfbao.request.premises.PremisesPartnerReqDto;
import com.yixun.qfbao.request.premises.GetAddressReqDto;
import com.yixun.qfbao.request.premises.GetPremisesCityGroupByPinyinReqDto;
import com.yixun.qfbao.request.premises.GetPremisesReqDto;
import com.yixun.qfbao.response.BaseKeyValueListResponse;
import com.yixun.qfbao.response.BaseKeyValueResponse;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.CommonResponse;
import com.yixun.qfbao.response.premises.PremisesDetailResDto;
import com.yixun.qfbao.response.premises.*;
import com.yixun.qfbao.response.premises.detail.*;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.FieldConvertUtil;
import com.yixun.qfbao.utils.HttpApi;
import com.yixun.qfbao.xf.mapper.ApartmentLayoutDiagramMapper;
import com.yixun.qfbao.xf.mapper.PremisesMapper;
import com.yixun.qfbao.xf.model.ApartmentLayoutDiagram;
import com.yixun.qfbao.xf.model.PremisesDynamic;
import com.yixun.qfbao.xf.model.dto.*;
import com.yixun.qfbao.xf.model.enums.AttetionStatusEnum;
import com.yixun.qfbao.xf.model.enums.PremisesImageEnum;
import com.yixun.qfbao.xf.model.enums.RelTypeEnum;
import com.yixun.qfbao.xf.model.enums.ReportWayEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.PremisesDynamicService;
import com.yixun.qfbao.xf.service.PremisesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Controller
@ResponseBody
@RequestMapping("/agency/weapp/premises")
@Api(tags = {"分销小程序-楼盘"})
public class PremisesController {

	private static final Logger LOGGER = LoggerFactory.getLogger(PremisesController.class);
	
    @Autowired
    CompanyService companyService;
    @Autowired
    PremisesService premisesService;
    @Autowired
    PremisesApiService premisesApiService;
    @Autowired
    BaseCodeValueService baseCodeValueService;
    @Autowired
    PremisesMapper premisesMapper;
    @Autowired
    PremisesImageMapper premisesImageMapper;
    @Autowired
    PremisesPartnerMapper premisesPartnerMapper;
    @Autowired
    ApartmentLayoutDiagramMapper apartmentLayoutDiagramMapper;
    @Autowired
    PremisesDynamicService premisesDynamicService;
    @Autowired
    AreaService areaService;
    @Autowired
    PartnerInfoService partnerInfoService;
    
    @ApiOperation(notes = "根据名称模糊匹配获取代理楼盘所有城市", value="根据名称模糊匹配获取代理楼盘所有城市")
    @PostMapping("/getPremisesCityByCityName")
    public BaseResponse<GetPremisesCityByCityNameResDto> getPremisesCityByCityName(@RequestBody @Valid BaseRequest<GetPremisesCityGroupByPinyinReqDto> request) throws Exception {
        BaseResponse<GetPremisesCityByCityNameResDto> response = new BaseResponse<>();

        GetPremisesCityGroupListReqDto reqDto = new GetPremisesCityGroupListReqDto();
        if(StringUtils.isBlank(request.getCompanyUuid())){
            response.setCode(ResultEnum.COMPANY_IS_NULL.getNo());
            response.setMsg(ResultEnum.COMPANY_IS_NULL.getDes());
            return response;
        }
        if(StringUtils.isBlank(request.getUserId())){
            response.setCode(ResultEnum.USER_IS_NULL.getNo());
            response.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return response;
        }
        PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(request.getUserId()));
        if (Objects.isNull(pi)) {
            response.setCode(ResultEnum.USER_IS_NULL.getNo());
            response.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return response;
        }

        reqDto.setMerchantId(pi.getHigherid());
        reqDto.setCityName(request.getData().getCityName());
        reqDto.setCompanyUuid(request.getCompanyUuid());
        reqDto.setLoginType(request.getLoginType());
        GetPremisesCityGroupListResDto oldResDto = premisesApiService.getPremisesCityGroupListService(reqDto);
        
        GetPremisesCityByCityNameResDto resData = new GetPremisesCityByCityNameResDto();
        for(PremisesCity premisesCity: oldResDto.getPremisesCityList()) {
        	premisesCity.setCityName(premisesCity.getJustCityName());
        	resData.addPremisesCity(premisesCity);
        }
        response.setData(resData);
        return response;
    }
    
    @ApiOperation(notes = "获取楼盘城市，按照首字母划分数组", value="获取楼盘城市，按照首字母划分数组")
    @PostMapping("/getPremisesCityGroupByPinyin")
    public BaseResponse<GetPremisesCityGroupByPinyinResDto> getPremisesCityGroupByPinyin(@RequestBody @Valid CommonReqDTO request) throws Exception {
        BaseResponse<GetPremisesCityGroupByPinyinResDto> response = new BaseResponse<>();

        GetPremisesCityGroupListReqDto reqDto = new GetPremisesCityGroupListReqDto();
        if(StringUtils.isBlank(request.getCompanyUuid())){
            response.setCode(ResultEnum.COMPANY_IS_NULL.getNo());
            response.setMsg(ResultEnum.COMPANY_IS_NULL.getDes());
            return response;
        }
        if(StringUtils.isBlank(request.getUserId())){
            response.setCode(ResultEnum.USER_IS_NULL.getNo());
            response.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return response;
        }
        PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(request.getUserId()));
        if (Objects.isNull(pi)) {
            response.setCode(ResultEnum.USER_IS_NULL.getNo());
            response.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return response;
        }

        reqDto.setMerchantId(pi.getHigherid());
        reqDto.setCompanyUuid(request.getCompanyUuid());
        reqDto.setLoginType(request.getLoginType());
        reqDto.setUserId(request.getUserId());
        GetPremisesCityGroupListResDto oldResDto = premisesApiService.getPremisesCityGroupListService(reqDto);
        
        GetPremisesCityGroupByPinyinResDto resData = new GetPremisesCityGroupByPinyinResDto();
        for(PremisesCity premisesCity: oldResDto.getPremisesCityList()) {
        	PremisesCityDto premisesCityDto = new PremisesCityDto();
        	premisesCityDto.setCityName(premisesCity.getJustCityName());
        	premisesCityDto.setCityId(premisesCity.getCityId());
        	String pinYin = premisesCity.getPinYin();
        	if(StringUtils.isBlank(pinYin) || "null".equals(pinYin)) {
        		pinYin = "";
        	}else {
        		pinYin = pinYin.substring(0, 1);
        	}
        	premisesCityDto.setPinyin(pinYin.toUpperCase());
        	resData.addPremisesCityDto(premisesCityDto);
        }
        if(resData.getList() != null && resData.getList().size() != 0) {
        	Collections.sort(resData.getList());
        }
        response.setData(resData);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }
    
    @ApiOperation(notes = "根据城市ID获取楼盘的区域集合", value="根据城市ID获取楼盘的区域集合")
    @PostMapping("/getPremisesAreaByCityId")
    public BaseResponse<GetPremisesAreaListResDto> getPremisesAreaByCityId(@RequestBody @Valid BaseRequest<GenericIdRequest> request) {
    	BaseResponse<GetPremisesAreaListResDto> res = new BaseResponse<>();
    	GetAreaAndPremisesCountReqDto getAreaAndPremisesCountReqDto = new GetAreaAndPremisesCountReqDto();
    	String userId = request.getUserId();
    	if(StringUtils.isBlank(userId)) {
    		res.setCode(ResultEnum.USER_IS_NULL.getNo());
    		res.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return res;
    	}
    	getAreaAndPremisesCountReqDto.setUserId(userId);
    	getAreaAndPremisesCountReqDto.setLoginType(request.getLoginType());
    	getAreaAndPremisesCountReqDto.setCityId(request.getData().getId());
    	String companyUuid = request.getCompanyUuid();
    	if(StringUtils.isBlank(companyUuid)) {
    		res.setCode(ResultEnum.COMPANY_IS_NULL.getNo());
    		res.setMsg(ResultEnum.COMPANY_IS_NULL.getDes());
            return res;
    	}
    	getAreaAndPremisesCountReqDto.setCompanyUuid(companyUuid);
    	GetPremisesAreaListResDto getPremisesAreaListResDto = areaService.getAreaAndPremisesCount(getAreaAndPremisesCountReqDto);
    	
    	res.setData(getPremisesAreaListResDto);
    	res.setCode(ResultEnum.SUCCESS.getNo());
		res.setMsg(ResultEnum.SUCCESS.getDes());
    	return res;
    }

    @ApiOperation(notes = "获取所有城市和区域的集合", value="获取所有城市和区域的集合")
    @PostMapping("/getPremisesAreaAndCityList")
    public BaseResponse<GetPremisesAreaAndCityListResDto> getPremisesAreaAndCityList(@RequestBody @Valid CommonReqDTO request) {
    	BaseResponse<GetPremisesAreaAndCityListResDto> res = new BaseResponse<>();
    	GetAreaAndPremisesCountReqDto getAreaAndPremisesCountReqDto = new GetAreaAndPremisesCountReqDto();
    	String userId = request.getUserId();
    	if(StringUtils.isBlank(userId)) {
    		res.setCode(ResultEnum.USER_IS_NULL.getNo());
    		res.setMsg(ResultEnum.USER_IS_NULL.getDes());
            return res;
    	}
    	getAreaAndPremisesCountReqDto.setUserId(userId);
    	getAreaAndPremisesCountReqDto.setLoginType(request.getLoginType());
//    	getAreaAndPremisesCountReqDto.setCityId(request.getData().getId());
    	String companyUuid = request.getCompanyUuid();
    	if(StringUtils.isBlank(companyUuid)) {
    		res.setCode(ResultEnum.COMPANY_IS_NULL.getNo());
    		res.setMsg(ResultEnum.COMPANY_IS_NULL.getDes());
            return res;
    	}
    	getAreaAndPremisesCountReqDto.setCompanyUuid(companyUuid);
    	GetPremisesAreaListResDto getPremisesAreaListResDto = areaService.getAreaAndPremisesCount(getAreaAndPremisesCountReqDto);
    	
    	GetPremisesAreaAndCityListResDto getPremisesAreaAndCityListResDto = new GetPremisesAreaAndCityListResDto(
    			getPremisesAreaListResDto.getPremisesAreaList());
    	res.setData(getPremisesAreaAndCityListResDto);
    	res.setCode(ResultEnum.SUCCESS.getNo());
		res.setMsg(ResultEnum.SUCCESS.getDes());
    	return res;
    }

    private static final int CODE_NAME_LIST_SIZE = 4;
    @ApiOperation(notes = "获取列表", value="获取楼盘列表")
    @PostMapping("/getAgentPremiseInfoPage")
    public BaseResponse<List<AgentPremiseInfoPageResDto>> getAgentPremiseInfoPage(@RequestBody @Valid PageRequest<GetPremisesReqDto> request){
        
    	FieldConvertUtil.listContainsNullToListNull(request.getSearchDTO());
        //入参
        GetAgentPremisesPageReqDto getAgentPremisesPageReqDto = buildGetAgentPremisesPageReqDto(request);
        
        //业务
        GetAgentPremisesPageResDto getAgentPremisesPageResDto = premisesApiService.getAgentPremisesPage(getAgentPremisesPageReqDto);
        
        //出参
        BaseResponse<List<AgentPremiseInfoPageResDto>> response = new BaseResponse<>();
        if(ResultEnum.FAIL.getNo().equals(getAgentPremisesPageResDto.getResult())) {
        	response.setCode(ResultEnum.FAIL.getNo());
        	response.setMsg(getAgentPremisesPageResDto.getResultMsg());
        	return response;
        }
        
        List<PremisesDto> premisesDtos = getAgentPremisesPageResDto.getGetAgentPremisesPage().getRows();
        List<AgentPremiseInfoPageResDto> returnData = new ArrayList<>();
        for (PremisesDto premisesDto : premisesDtos) {
        	AgentPremiseInfoPageResDto agentPremiseInfoPageResDto = new AgentPremiseInfoPageResDto();
        	agentPremiseInfoPageResDto.setPremiseId(premisesDto.getId());
        	agentPremiseInfoPageResDto.setPremisesName(premisesDto.getName());
        	agentPremiseInfoPageResDto.setProvName(premisesDto.getProvinceName());
        	agentPremiseInfoPageResDto.setCityName(premisesDto.getCityName());
        	agentPremiseInfoPageResDto.setAreaName(premisesDto.getAreaName());
        	agentPremiseInfoPageResDto.setMainPush(premisesDto.getMainPush() == null? 0: premisesDto.getMainPush());
        	agentPremiseInfoPageResDto.setUrl(premisesDto.getUrl());
        	agentPremiseInfoPageResDto.setViedoFlag(0);
        	agentPremiseInfoPageResDto.setVrFlag(StringUtils.isBlank(premisesDto.getPanoramicView())? 0: 1);
        	LOGGER.info(String.valueOf(premisesDto.getCreateTime()));
        	List<BigDecimal> areaRange = StringUtil.getMinOrMaxFromSeprateString(
        			premisesDto.getApartmentArea(), ",", 3);
        	if(areaRange != null) {
        		agentPremiseInfoPageResDto.setMinArea(areaRange.get(0));
        		agentPremiseInfoPageResDto.setMaxArea(areaRange.get(1));
        	}
        	List<String> codeNameList = StringUtil.getAppointNumberListFromSeprateString(
        			premisesDto.getCodeNameList(), ",", CODE_NAME_LIST_SIZE);
        	agentPremiseInfoPageResDto.setCodeName(codeNameList);
        	List<BigDecimal> minPrice = StringUtil.getMinOrMaxFromSeprateString(
        			premisesDto.getAveragePrice(), ",", 1);
        	agentPremiseInfoPageResDto.setPrice(minPrice == null? new BigDecimal(0): minPrice.get(0));
        	returnData.add(agentPremiseInfoPageResDto);
		}
        response.setData(returnData);
        response.setCode(getAgentPremisesPageResDto.getResult());
        response.setMsg(getAgentPremisesPageResDto.getResultMsg());
        return response;
    }
    

    /**
     * 	根据输入的名称和客户信息获取对应的楼盘名称和ID
     * 	注： 一次最多展示多少楼盘名称
     * @param request
     * @return
     */
	@ApiOperation(notes = "获取联想楼盘名称", value="获取联想楼盘名称")
	@PostMapping("/getPremisesName")
	public BaseResponse<GetPremisesIdAndNameResDto> getPremisesIdAndName(@RequestBody @Valid PageRequest<GetPremisesReqDto> request){
	    BaseResponse<GetPremisesIdAndNameResDto> response = new BaseResponse<>();
	    GetAgentPremisesPageReqDto getAgentPremisesPageReqDto = buildGetAgentPremisesPageReqDto(request);
        
        GetAgentPremisesPageResDto getAgentPremisesPageResDto = premisesApiService.getAgentPremisesPage(getAgentPremisesPageReqDto);
	    
        List<PremisesDto> premisesDtos = getAgentPremisesPageResDto.getGetAgentPremisesPage().getRows();
        GetPremisesIdAndNameResDto getPremisesIdAndNameResDto = new GetPremisesIdAndNameResDto();
        for (PremisesDto premisesDto : premisesDtos) {
        	PremisesIdAndNameResDto premisesIdAndNameResDto = new PremisesIdAndNameResDto();
        	premisesIdAndNameResDto.setPremisesId(premisesDto.getId());
        	premisesIdAndNameResDto.setPremisesName(premisesDto.getName());
			premisesIdAndNameResDto.setPremisesAddress(premisesDto.getPremisesAddress());
			premisesIdAndNameResDto.setReportWay(StringUtil.isNotNull(premisesDto.getReportWay())? 
					premisesDto.getReportWay(): ReportWayEnum.FULL_NUMBER.getNo());
			premisesIdAndNameResDto.setReportWayName(StringUtil.isNotNull(premisesDto.getReportWay())? 
					ReportWayEnum.getDescByNo(premisesDto.getReportWay()): ReportWayEnum.FULL_NUMBER.getDes());
			if (Objects.nonNull(premisesDto) && !TimeUnitTypeEnum.NONE.getNo().equals(premisesDto.getSpecialTimeType())
			&& StringUtils.isNotBlank(premisesDto.getSpecialTime())) {
				premisesIdAndNameResDto.setSpecialTime(premisesDto.getSpecialTime());
			}
        	getPremisesIdAndNameResDto.addPremisesIdAndNameResDto(premisesIdAndNameResDto);
		}
        response.setData(getPremisesIdAndNameResDto);
        response.setCode(getAgentPremisesPageResDto.getResult());
        response.setMsg(getAgentPremisesPageResDto.getResultMsg());
        return response;
	}
	
	/**
	 * 获取楼盘名称以及获取楼盘列表公用的构造楼盘传入service入参方法
	 * @param request
	 * @return
	 */
	private GetAgentPremisesPageReqDto buildGetAgentPremisesPageReqDto(PageRequest<GetPremisesReqDto> request) {
		GetPremisesReqDto getPremisesReqDto = request.getSearchDTO();
	    GetAgentPremisesPageReqDto getAgentPremisesPageReqDto = new GetAgentPremisesPageReqDto();
		BeanUtils.copyProperties(getPremisesReqDto, getAgentPremisesPageReqDto);
        getAgentPremisesPageReqDto.setUserId(request.getUserId());
        getAgentPremisesPageReqDto.setLoginType(request.getLoginType());
        getAgentPremisesPageReqDto.setCompanyUuid(request.getCompanyUuid());
        getAgentPremisesPageReqDto.setName(getPremisesReqDto.getPremisesName());
        getAgentPremisesPageReqDto.setPageNo(request.getPageDTO().getPageNum());
        getAgentPremisesPageReqDto.setPageSize(request.getPageDTO().getPageSize());
        return getAgentPremisesPageReqDto;
	}
    
	/**
	 * 关注楼盘，不存在则插入，存在则更新。并校验传递参数
	 * @param reqDto
	 * @return
	 */
	@ApiOperation(value="关注/取关楼盘")
	@PostMapping("/attetionPremises")
	@History(sinceTime = "2020-12-28")
	public CommonResponse attetionPremises(@RequestBody @Valid BaseRequest<PremisesPartnerReqDto> reqDto) {
		PremisesPartnerDto premisesAttetionDto = new PremisesPartnerDto();
		BeanUtils.copyProperties(reqDto.getData(), premisesAttetionDto);
		premisesAttetionDto.setPartnerId(Long.parseLong(reqDto.getUserId()));
		String result = premisesApiService.updatePremisesPartnerRel(premisesAttetionDto);
		
		CommonResponse commonResponse = new CommonResponse();
		commonResponse.setMsg(result);
		commonResponse.setCode(result.indexOf("成功") > -1? ResultEnum.SUCCESS.getNo(): ResultEnum.FAIL.getNo());
		return commonResponse;
	}
	
	/**
	 * 关注楼盘，不存在则插入，存在则更新。并校验传递参数
	 * @param reqDto
	 * @return
	 */
	@ApiOperation(value="更改设置 关注/取关/店铺收藏/取消店铺收藏")
	@PostMapping("/updatePremisesPartnerRel")
	public CommonResponse updatePremisesPartnerRel(@RequestBody @Valid BaseRequest<PremisesPartnerReqDto> reqDto) {
		PremisesPartnerDto premisesAttetionDto = new PremisesPartnerDto();
		BeanUtils.copyProperties(reqDto.getData(), premisesAttetionDto);
		premisesAttetionDto.setPartnerId(Long.parseLong(reqDto.getUserId()));
		String result = premisesApiService.updatePremisesPartnerRel(premisesAttetionDto);
		
		CommonResponse commonResponse = new CommonResponse();
		commonResponse.setMsg(result);
		commonResponse.setCode(result.indexOf("成功") > -1? ResultEnum.SUCCESS.getNo(): ResultEnum.FAIL.getNo());
		return commonResponse;
	}
	
	/**
	 * 获取当前公司对应的标签
	 * @param reqDto
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value="根据公司ID获取标签集合")
	@PostMapping("/getCompanyCodeList")
	public BaseResponse<BaseKeyValueListResponse> getCompanyCodeList(@RequestBody @Valid BaseRequest<GenericIdRequest> reqDto) throws Exception {
		GetBaseCodeValueListReqDto getBaseCodeValueListReqDto = new GetBaseCodeValueListReqDto();
		getBaseCodeValueListReqDto.setMerchantId(reqDto.getData().getId());
		
		GetBaseCodeValueListResDto getBaseCodeValueListResDto = 
				baseCodeValueService.getBaseCodeValueList(getBaseCodeValueListReqDto);
		
		BaseResponse<BaseKeyValueListResponse> res = new BaseResponse<>();
		List<BaseCodeValue> baseCodeValueList = getBaseCodeValueListResDto.getBaseCodeValueList();
		BaseKeyValueListResponse baseKeyValueListResponse = new BaseKeyValueListResponse();
		for (BaseCodeValue baseCodeValue : baseCodeValueList) {
			BaseKeyValueResponse baseKeyValueResponse = new BaseKeyValueResponse();
			baseKeyValueResponse.setId(String.valueOf(baseCodeValue.getId()));
			baseKeyValueResponse.setValue(baseCodeValue.getCodeName());
			baseKeyValueListResponse.addBaseKeyValueResponse(baseKeyValueResponse);
		}
		res.setData(baseKeyValueListResponse);
		res.setCode(ResultEnum.SUCCESS.getNo());
		res.setMsg(ResultEnum.SUCCESS.getDes());
		return res;
	}
	
	/**
	 *  根据楼盘ID获取楼盘详情
	 * @throws UnsupportedEncodingException
	 */
	@ApiParam(value = "根据楼盘ID获取详情")
	@PostMapping("/getPremisesDetail")
	public BaseResponse<PremisesDetailResDto> getPremisesDetail(@RequestBody @Valid BaseRequest<GenericIdRequest> reqDto) {
		//入参
		LOGGER.info("进入查询详情方法");
		Long id = reqDto.getData().getId();
		GetPremisesDetailReqDto getPremisesDetailReqDto = new GetPremisesDetailReqDto();
		getPremisesDetailReqDto.setId(id);
		getPremisesDetailReqDto.setLoginType(reqDto.getLoginType());
		// 使用原先的查看详情方法，然后重新组装数据
		GetPremisesDetailResDto getPremisesDetailResDto = premisesApiService.getPremisesDetail(getPremisesDetailReqDto);
		//组装出参
		com.yixun.qfbao.apidto.PremisesDetailResDto detailResDto = getPremisesDetailResDto.getDetailResDto();
		BaseResponse<PremisesDetailResDto> res = new BaseResponse<>();
		if(detailResDto == null) {
			res.setMsg("未查询到详情信息");
			res.setCode(ResultEnum.FAIL.getNo());
			return res;
		}
		PremisesDetailResDto premisesDetailResDto = new PremisesDetailResDto();
		
		//头部图片信息
		premisesDetailResDto.setHeaderDisplayUrlInfo(new HeaderDisplayUrlInfo(detailResDto));
		
		//图片下的详细信息以及点击全部进去之后的信息
		premisesDetailResDto.setDetailInfo(new DetailInfo(detailResDto));
		
		//案场人员信息
		if(detailResDto.getCaseMemberName() != null && detailResDto.getCaseMemberPhone() != null) {
			premisesDetailResDto.setCaseMemberInfo(new CaseMemberInfo(detailResDto));
		}
		
		//楼盘动态信息
		if(detailResDto.getPreDynamicList() != null && detailResDto.getPreDynamicList().size() > 0) {
			premisesDetailResDto.setPremisesDynamicInfo(new PremisesDynamicInfo(detailResDto.getPreDynamicList().get(0)));
		}
		
		//佣金信息
		premisesDetailResDto.setCommisionInfos(CommisionInfo.convertCommisionStrToList(detailResDto.getCommission()));
		
		//奖金信息
		String reward = detailResDto.getReward();
		String dealReward = detailResDto.getDealAward();
		String rewardType = detailResDto.getDealType();
		if((StringUtils.isBlank(reward) || "null".equals(reward) || "0".equals(reward)) 
				&& (StringUtils.isBlank(dealReward) || "null".equals(dealReward) || "0".equals(dealReward)) 
				&& (StringUtils.isBlank(rewardType) || "null".equals(rewardType))) {
		}else {
			premisesDetailResDto.setRewardInfo(new RewardInfo(detailResDto));
		}
		
		//规则信息
		premisesDetailResDto.setRuleInfo(new RuleInfo(detailResDto));
		
		//销售信息
		if(StringUtils.isBlank(detailResDto.getSelling()) && StringUtils.isBlank(detailResDto.getCustomer())
				&& StringUtils.isBlank(detailResDto.getIntroduction()) && StringUtils.isBlank(detailResDto.getSaleSkill())){
		}else {
			premisesDetailResDto.setSellInfo(new SellInfo(detailResDto));
		}
		
		//户型信息
		ApartmentLayoutDiagramDto apartmentLayoutDiagramDto = new ApartmentLayoutDiagramDto();
		apartmentLayoutDiagramDto.setPremisesId(id);
		apartmentLayoutDiagramDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<ApartmentLayoutDiagram> apartmentInfos = apartmentLayoutDiagramMapper.selectListByDto(apartmentLayoutDiagramDto);
		for (ApartmentLayoutDiagram apartmentLayoutDiagram : apartmentInfos) {
			premisesDetailResDto.addApartmentInfo(new ApartmentInfo(apartmentLayoutDiagram));
		}
		
		premisesDetailResDto.addPremisesFileInfos(getPremisesDetailResDto.getPremisesImages());
		
		//关注信息
		// userId不传的时候可能是分享的页面，没有登录也可以访问
		PremisesPartnerDto premisesAttetionDto = new PremisesPartnerDto();
		int attetionFlag = AttetionStatusEnum.cancel_attetion.getNo();
		int shopFlag = AttetionStatusEnum.cancel_attetion.getNo();
		if(StringUtils.isNotEmpty(reqDto.getUserId())) {
			premisesAttetionDto.setPartnerId(Long.parseLong(reqDto.getUserId()));
			premisesAttetionDto.setPremisesId(id);
			premisesAttetionDto.setRelFlag(AttetionStatusEnum.set_attetion.getNo());
			// 判断关注或者店铺
			List<PremisesPartner> premisesPartners = premisesPartnerMapper.selectByDto(premisesAttetionDto);
			if(CollectionUtils.isNotEmpty(premisesPartners)) {
				for (PremisesPartner premisesPartner : premisesPartners) {
					if(RelTypeEnum.shop.getNo() == premisesPartner.getRelType()) {
						shopFlag = AttetionStatusEnum.set_attetion.getNo();
						continue;
					}
					if(RelTypeEnum.attention.getNo() == premisesPartner.getRelType()) {
						attetionFlag = AttetionStatusEnum.set_attetion.getNo();
					}
				}
			}
		}
		premisesDetailResDto.setAttetionFlag(attetionFlag);
		premisesDetailResDto.setShopFlag(shopFlag);
		
		premisesDetailResDto.setReportWay(detailResDto.getReportWay());
		
		premisesDetailResDto.setUrl(detailResDto.getUrl());
		premisesDetailResDto.setSpecialTime(detailResDto.getSpecialTime());
		
		res.setData(premisesDetailResDto);
		res.setMsg(ResultEnum.SUCCESS.getDes());
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}
	
	/**
	 * 根据楼盘ID查询楼盘相册
	 * @param reqDto
	 * @return
	 */
	@ApiParam(value = "根据楼盘ID查询全部的楼盘相册")
	@PostMapping(value = "/getAllImageByPremisesId")
	public BaseResponse<PremisesImgInfoResDto> getAllImageByPremisesId(@RequestBody @Valid BaseRequest<GenericIdRequest> reqDto){
		List<PremisesPhotoDto> premisesPhotoDtos = premisesImageMapper.selectPremisesPhono(reqDto.getData().getId());
		PremisesImgInfoResDto premisesImgInfoReqDto = new PremisesImgInfoResDto();
		for (PremisesPhotoDto premisesPhotoDto : premisesPhotoDtos) {
			String photoType = premisesPhotoDto.getPhotoType();
			if("3".equals(photoType) || "4".equals(photoType) || "5".equals(photoType)
					|| "6".equals(photoType) || "7".equals(photoType)) {
				ImgInfoResDto imgInfoResDto = new ImgInfoResDto();
				imgInfoResDto.setCount(premisesPhotoDto.getPhotoCount());
				imgInfoResDto.setImageType(PremisesImageEnum.getDesByNo(photoType));
				imgInfoResDto.setImageUrlList(ImgInfoResDto.spiltStringToUrlList(
						premisesPhotoDto.getPhotoListStr(), ","));
				premisesImgInfoReqDto.addImgInfoResDto(imgInfoResDto);
			}
		}
		BaseResponse<PremisesImgInfoResDto> res = new BaseResponse<>();
		res.setData(premisesImgInfoReqDto);
		res.setMsg(ResultEnum.SUCCESS.getDes());
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}

	//获取楼盘动态。注意分页
	@ApiParam(value = "根据楼盘ID分页查询当前楼盘可展示的动态")
	@PostMapping(value = "/getPageDynamicByPremisesId")
	public BaseResponse<PremisesDynamicListResDto> getPageDynamicByPremisesId(@RequestBody PageRequest<GenericIdRequest> reqDto) {
		PageDTO pageDto = reqDto.getPageDTO();
		int size = pageDto.getPageSize();
		int num = pageDto.getPageNum();
		int begin = (num - 1) * size;
		PremisesDynamicDto premisesDynamicDto = new PremisesDynamicDto();
		premisesDynamicDto.setPremisesId(reqDto.getSearchDTO().getId());
		premisesDynamicDto.setCanSee(true);
		premisesDynamicDto.setOrders(" t.update_time desc ");
		Page<PremisesDynamic> premisesDynamicPage = premisesDynamicService.getPageModel(premisesDynamicDto, begin, size);
		
		List<PremisesDynamic> premisesDynamics = premisesDynamicPage.getRows();
		PremisesDynamicListResDto premisesDynamicListResDto = new PremisesDynamicListResDto();
		BaseResponse<PremisesDynamicListResDto> res = new BaseResponse<>();
		for (PremisesDynamic premisesDynamic : premisesDynamics) {
			premisesDynamicListResDto.addPremisesDynamicResDto(new PremisesDynamicResDto(premisesDynamic));
		}
		res.setData(premisesDynamicListResDto);
		return res;
	}
	
	/**
	 * 根据经纬度获取地市
	 * @param reqDto
	 * @return
	 */
	@PostMapping("/getAddress")
	@ApiParam(value = "根据经纬度获取地市和区域的名称")
    public BaseResponse<GetAddressResDto> getAddress(@RequestBody BaseRequest<GetAddressReqDto> reqDto){
		BaseResponse<GetAddressResDto> response = new BaseResponse<>();
		GetAddressReqDto getAddressReqDto = reqDto.getData();
        String url = "https://apis.map.qq.com/ws/geocoder/v1/";
        String params = "location="+getAddressReqDto.getLatitude()+","+getAddressReqDto.getLongitude()+"&key=TH5BZ-TFQ3W-LZORS-RI7ZM-C2REE-3EBRD&get_poi=0";
        String result = HttpApi.sendGet(url,params,null);
        
        GetAddressResDto getAddressResDto = new GetAddressResDto();
        JSONObject jsonObject = JSONObject.parseObject(result);
        
		try {
			JSONObject adInfo = jsonObject.getJSONObject("result").getJSONObject("ad_info");
			String city = adInfo.getString("city");
	        String district = adInfo.getString("district");
	        getAddressResDto.setCityName(city == null? "": city);
	        getAddressResDto.setDistrictName(district == null? "": district);
		} catch (Exception e) {
			getAddressResDto.setCityName("");
	        getAddressResDto.setDistrictName("");
		}
        response.setData(getAddressResDto);
        response.setMsg(ResultEnum.SUCCESS.getDes());
        response.setCode(ResultEnum.SUCCESS.getNo());
        return response;
    }
}
